using Super;
using System;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// OnMouseDown 这类方法只能用去带有collider的对象上
/// </summary>
public class MultiTree_AABB_Sample : MonoBehaviour
{

	public GO_Generator generator;
	public MultiTree<AABBNodeValueSample> tree = new MultiTree<AABBNodeValueSample>();
	private Vector3 downPoint = Vector3.zero;
	private Vector3 upPoint = Vector3.zero;
	private Bounds bounds = new Bounds();
	private bool isMouseDown = false;
	private Plane plane;
	void Start()
	{
		plane = new Plane(Vector3.forward, generator.parent.transform.position);
		generator.GenerateCallBack = InserTree;
		generator.Generate();
		for (int i = 0; i < 10; i++)
		{
			SetColor(i, GlobalUtil.GetColor(i));
		}
	}

	public void OnEnable()
	{
		EventManager.Register<MouseDownGE>(OnMouseDown);
		EventManager.Register<MouseUpGE>(OnMouseUp);
	}

	public void OnDestroy()
	{
		EventManager.UnRegisterAll(this);
	}

	void InserTree(GameObject go)
	{
		var node = new AABBNodeValueSample();
		node.GO = go;
		node.Bound = go.GetComponent<BoxCollider2D>().bounds;
		tree.Insert(node);
	}
	public void SetColor(object key, Color color)
	{
		List<object> keys = new List<object>() { key };
		var node = tree.Find(keys);
		List<MultiTreeNode<AABBNodeValueSample>> list = tree.GetAllNodeByNode(node);
		foreach (var item in list)
		{
			item.value.GO.GetComponent<SpriteRenderer>().color = color;
		}
	}
	public void OnDrawGizmos()
	{
		List<MultiTreeNode<AABBNodeValueSample>> list = tree.GetAllNode(false);
		foreach (var item in list)
		{
			Gizmos.color = GlobalUtil.GetColor(item.height);
			Gizmos.DrawWireCube(item.value.Bound.center, item.value.Bound.size);
		}

		if (isMouseDown)
		{
			upPoint = GlobalUtil.GetMouseInPlanePos(plane);
			bounds = new Bounds();
			bounds.center = (upPoint + downPoint) / 2;
			bounds.size = new Vector3(MathF.Abs(upPoint.x - downPoint.x), MathF.Abs(upPoint.y - downPoint.y), 0.01f);
			Color color = Color.green;
			color.a = 0.8f;
			Gizmos.color = color;
			Gizmos.DrawCube(bounds.center, bounds.size);
		}
	}

	public void OnMouseDown(MouseDownGE eventData)
	{
		if (eventData.key == MouseKey.Left)
		{
			isMouseDown = true;
			downPoint = GlobalUtil.GetMouseInPlanePos(plane);
		}
	}

	public void OnMouseUp(MouseUpGE eventData)
	{
		if (eventData.key != MouseKey.Left) return;

		isMouseDown = false;
		upPoint = GlobalUtil.GetMouseInPlanePos(plane);

		bounds = new Bounds();
		bounds.center = (upPoint + downPoint) / 2;
		bounds.size = new Vector3(MathF.Abs(upPoint.x - downPoint.x), MathF.Abs(upPoint.y - downPoint.y), 0);

		foreach (var item in generator.pool.usePool)
		{
			item.GetComponent<SpriteRenderer>().color = UnityEngine.Color.white;
		}

		List<MultiTreeNode<AABBNodeValueSample>> list = tree.GetAllNode(SelectCondition);
		foreach (var item in list)
		{
			item.value.GO.GetComponent<SpriteRenderer>().color = UnityEngine.Color.green;
		}
	}

	public bool SelectCondition(MultiTreeNode<AABBNodeValueSample> node)
	{
		if (bounds.Intersects(node.value.Bound))
			return true;
		return false;
	}
}
